package com.aihongbo.service.t1;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class LogService1 extends Service {

    private String TAG = "aihongbo";

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    private int serviceStatus = 0; //服务运行状态
    private LogBinder binder = new LogBinder();

    Thread task = new Thread(() -> {
        while (true) {
            try {
                Thread.sleep(1000);
                if (serviceStatus == 1) {
                    Log.d(TAG, simpleDateFormat.format(new Date()));
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    });

    @Override
    public void onCreate() {
        super.onCreate();
        task.start();
    }

    public class LogBinder extends Binder {
        public LogService1 getService() {
            return LogService1.this;
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    public void start() {
        serviceStatus = 1;
    }

    public void stop() {
        serviceStatus = 0;
    }
}
